Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-timeout

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-timeout

Simple timeouts for promises

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is promise-timeout?

The `promise-timeout` npm package provides utilities to add timeout functionality to promises. It allows you to set a time limit on a promise, after which it will be rejected if not resolved. This is useful for handling cases where you want to ensure that certain asynchronous operations do not take longer than a specified duration.

What are promise-timeout's main functionalities?

Timeout a Promise

This feature allows you to set a timeout on a promise. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with a timeout error.

const { timeout } = require('promise-timeout');

const myPromise = new Promise((resolve) => {
  setTimeout(() => resolve('Success!'), 2000);
});

const timeoutPromise = timeout(myPromise, 1000);

timeoutPromise
  .then((result) => console.log(result))
  .catch((err) => console.error('Promise timed out:', err));

Timeout with Custom Error

This feature allows you to set a timeout on a promise with a custom error message. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with the custom timeout error.

const { timeout, TimeoutError } = require('promise-timeout');

const myPromise = new Promise((resolve) => {
  setTimeout(() => resolve('Success!'), 2000);
});

const timeoutPromise = timeout(myPromise, 1000, new TimeoutError('Custom timeout error'));

timeoutPromise
  .then((result) => console.log(result))
  .catch((err) => console.error('Promise timed out:', err.message));

Other packages similar to promise-timeout

Keywords

FAQs

Package last updated on 18 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc